package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.Publisher;
import com.ruoyi.system.mapper.PublisherMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.MovieBo;
import com.ruoyi.system.domain.vo.MovieVo;
import com.ruoyi.system.domain.Movie;
import com.ruoyi.system.mapper.MovieMapper;
import com.ruoyi.system.service.IMovieService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-04
 */
@RequiredArgsConstructor
@Service
public class MovieServiceImpl implements IMovieService {

    private final MovieMapper baseMapper;

    private final PublisherMapper publisherMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public MovieVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<MovieVo> queryPageList(MovieBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Movie> lqw = buildQueryWrapper(bo);
        Page<MovieVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<MovieVo> queryList(MovieBo bo) {
        LambdaQueryWrapper<Movie> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Movie> buildQueryWrapper(MovieBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Movie> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPublisherId() != null, Movie::getPublisherId, bo.getPublisherId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), Movie::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Movie::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getEnglishName()), Movie::getEnglishName, bo.getEnglishName());
        lqw.eq(StringUtils.isNotBlank(bo.getLanguage()), Movie::getLanguage, bo.getLanguage());
        lqw.eq(bo.getTime() != null, Movie::getTime, bo.getTime());
        lqw.eq(StringUtils.isNotBlank(bo.getPlace()), Movie::getPlace, bo.getPlace());
        lqw.eq(bo.getStartTime() != null, Movie::getStartTime, bo.getStartTime());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(MovieBo bo) {
        Movie add = BeanUtil.toBean(bo, Movie.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(MovieBo bo) {
        Movie update = BeanUtil.toBean(bo, Movie.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Movie entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<MovieVo> getList(Integer type) {
        List<MovieVo> result = new ArrayList<>();
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        List<Movie> movies = baseMapper.selectList(wrapper);
        for (int i = 0; i < movies.size(); i++) {
            MovieVo vo = new MovieVo();
            BeanUtil.copyProperties(movies.get(i), vo);

            Long pid = movies.get(i).getPublisherId();
            Publisher publisher = publisherMapper.selectById(pid);
            vo.setPublisherName(publisher.getName());
            result.add(vo);
        }
        return result;
    }


}
